/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.sprite_nodes
{
	import org.cocos2d.cocoa.CCObject;
	import org.cocos2d.cocoa.CCPoint;
	import org.cocos2d.cocoa.CCRect;
	import org.cocos2d.cocoa.CCSize;
	import org.cocos2d.cocoa.CCZone;
	import org.cocos2d.support.ccMacros;
	import org.cocos2d.textures.CCTexture2D;
	import org.cocos2d.textures.CCTextureCache;

	public class CCSpriteFrame extends CCObject
	{
		protected var m_obRectInPixels : CCRect;
		
		public function get RectInPixels() : CCRect
		{
			return m_obRectInPixels;
		}
		
		public function set RectInPixels(rectInPixels : CCRect) : void
		{
			m_obRectInPixels = rectInPixels;
		}
		
		protected var m_bRotated : Boolean;
		
		public function get Rotated() : Boolean
		{
			return m_bRotated;
		}
		
		public function set Rotated(bRotated : Boolean) : void
		{
			m_bRotated = bRotated;
		}
		
		protected var m_obRect : CCRect;
		
		public function get Rect() : CCRect
		{
			return m_obRect;
		}
		
		public function set Rect(rect : CCRect) : void
		{
			m_obRect = rect;
			m_obRectInPixels = ccMacros.CC_RECT_PIXELS_TO_POINTS(m_obRect);
		}
		
		protected var m_obOffsetInPixels : CCPoint;
		
		public function get OffsetInPixels() : CCPoint
		{
			return m_obOffsetInPixels;
		}
		
		public function set OffsetInPixels(offsetInPixels : CCPoint) : void
		{
			m_obOffsetInPixels = offsetInPixels;
			m_obOffset = ccMacros.CC_POINT_PIXELS_TO_POINTS(m_obOffsetInPixels);
		}
		
		protected var m_obOriginalSizeInPixels;
		
		public function get OriginalSizeInPixels() : CCSize
		{
			return m_obOriginalSizeInPixels;
		}
		
		public function set OriginalSizeInPixels(sizeInPixels : CCSize) : void
		{
			m_obOriginalSizeInPixels = sizeInPixels;
		}
		
		protected var m_obOriginalSize : CCSize;
		
		public function get OriginalSize() : CCSize
		{
			return m_obOriginalSize;
		}
		
		public function set OriginalSize(sizeInPixels : CCSize) : void
		{
			m_obOriginalSize = sizeInPixels;
		}
		
		protected var m_pobTexture : CCTexture2D;
		protected var m_strTextureFilename : String;
		
		public function get Texture() : CCTexture2D
		{
			if (m_pobTexture)
			{
				return m_pobTexture;
			}
			
			if (m_strTextureFilename != null && m_strTextureFilename.length() > 0)
			{
				return CCTextureCache.sharedTextureCache().addImage(m_strTextureFilename);
			}
			
			return null;
		}
		
		public function set Texture(texture : CCTexture2D) : void
		{
			if (m_pobTexture != texture)
			{
				m_pobTexture = texture;
			}
		}
		
		protected var m_obOffset : CCPoint;
		
		public function get Offset() : CCPoint
		{
			return m_obOffset;
		}
		
		public function set Offset(offsets : CCPoint) : void
		{
			m_obOffset = offsets;
			m_obOffsetInPixels = ccMacros.CC_POINT_POINTS_TO_PIXELS(m_obOffset);
		}
		
		public override function copyWithZone(pZone : CCZone) : CCObject
		{
			var pCopy : CCSpriteFrame = new CCSpriteFrame();
			
			pCopy.initWithTextureFilename(m_strTextureFilename, m_obRectInPixels, m_bRotated, m_obOffsetInPixels, m_obOriginalSizeInPixels);
			pCopy.Texture = m_pobTexture;
			return pCopy;
		}
		
		[Deprecated]
		public static function frameWithTexture(pobTexture : CCTexture2D, rect : CCRect) : CCSpriteFrame
		{
			return CCSpriteFrame.create(pobTexture, rect);
		}
		
		[Deprecated]
		public static function frameWithTextureFilename(filename : String, rect : CCRect) : CCSpriteFrame
		{
			return CCSpriteFrame.create(filename, rect);
		}
		
		[Deprecated]
		public static function frameWithTexture(pobTexture : CCTexture2D, rect : CCRect, rotated : Boolean, offset : CCPoint, originalSize : CCSize) : CCSpriteFrame
		{
			return CCSpriteFrame.create(pobTexture, rect, rotated, offset, originalSize);
		}
		
		[Deprecated]
		public static function frameWithTextureFilename(filename : String, rect : CCRect, rotated : Boolean, offset : CCPoint, originalSize : CCSize) : CCSpriteFrame
		{
			return CCSpriteFrame.create(filename, rect, rotated, offset, originalSize);
		}
		
		public static function create(pobTexture : CCTexture2D, rect : CCRect) : CCSpriteFrame
		{
			var pSpriteFrame : CCSpriteFrame = new CCSpriteFrame();
			pSpriteFrame.initWithTexture(pobTexture, rect);
			
			return pSpriteFrame;
		}
		
		public static function create(filename : String, rect : CCRect) : CCSpriteFrame
		{
			var pSpriteFrame : CCSpriteFrame = new CCSpriteFrame();
			pSpriteFrame.initWithTextureFilename(filename, rect);
			
			return pSpriteFrame;
		}
		
		public static function create(pobTexture : CCTexture2D, rect : CCRect, rotated : Boolean, offset : CCPoint, originalSize : CCSize) : CCSpriteFrame
		{
			var pSpriteFrame : CCSpriteFrame = new CCSpriteFrame();
			pSpriteFrame.initWithTexture(pobTexture, rect, rotated, offset, originalSize);
			
			return pSpriteFrame;
		}
		
		public static function create(filename : String, rect : CCRect, rotated : Boolean, offset : CCPoint, originalSize : CCSize) : CCSpriteFrame
		{
			var pSpriteFrame : CCSpriteFrame = new CCSpriteFrame();
			pSpriteFrame.initWithTextureFilename(filename, rect, rotated, offset, originalSize);
			
			return pSpriteFrame;
		}
		
		public function initWithTexture(pobTexture : CCTexture2D, rect : CCRect) : Boolean
		{
			var rectInPixels : CCRect = ccMacros.CC_RECT_POINTS_TO_PIXELS(rect);
			return initWithTexture(pobTexture, rectInPixels, false, CCPoint.Zero, rectInPixels.size);
		}
		
		public function initWithTextureFilename(filename : String, rect : CCRect) : Boolean
		{
			var rectInPixels : CCRect = ccMacros.CC_RECT_POINTS_TO_PIXELS(rect);
			return initWithTextureFilename(filename, rectInPixels, false, CCPoint.Zero, rectInPixels.size);
		}
		
		public function initWithTexture(pobTexture : CCTexture2D, rect : CCRect, rotated : Boolean, offset : CCPoint, originalSize : CCSize) : Boolean
		{
			m_pobTexture = pobTexture;
			
			if (pobTexture)
			{
				// do nothing
			}
			
			m_obRectInPixels = rect;
			m_obRect = ccMacros.CC_RECT_PIXELS_TO_POINTS(rect);
			m_obOffsetInPixels = offset;
			m_obOffset = ccMacros.CC_POINT_PIXELS_TO_POINTS(m_obOffsetInPixels);
			m_obOriginalSizeInPixels = originalSize;
			m_obOriginalSize = ccMacros.CC_SIZE_PIXELS_TO_POINTS(m_obOriginalSizeInPixels);
			m_bRotated = rotated;
			
			return true;
		}
		
		public function initWithTextureFilename(filename : String, rect : CCRect, rotated : Boolean, offset : CCPoint, originalSize : CCSize) : Boolean
		{
			m_pobTexture = null;
			m_strTextureFilename = filename;
			m_obRectInPixels = rect;
			m_obRect = ccMacros.CC_RECT_PIXELS_TO_POINTS(rect);
			m_obOffsetInPixels = offset;
			m_obOffset = ccMacros.CC_POINT_PIXELS_TO_POINTS(m_obOffsetInPixels);
			m_obOriginalSizeInPixels = originalSize;
			m_obOriginalSize = ccMacros.CC_SIZE_PIXELS_TO_POINTS(m_obOriginalSizeInPixels);
			m_bRotated = rotated;
			
			return true;
		}
	}
}